new Vue({
	el: '#app',
	data: {
		uid: '',
		sid: '',
		day: '',
		orderList: []
	},
	mounted: function() {
		console.log("mounted end");
		this.uid = $api.getStorage('uid');
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			apiready = function() {
				var fid = api.pageParam;
				console.log(fid.sid);
				self.sid = fid.sid;
				var now = new Date();
				var day = now.getDay();
				self.setDay(day);
			}
		},
		getWeek: function(d) {
			var now = new Date();
			var nowTime = now.getTime();
			var day = now.getDay();
			console.log(day)
			var oneDayLong = 24 * 60 * 60 * 1000;

			//上周日
			var SundayTime = nowTime - (day) * oneDayLong;
			//周一到周六
			var MondayTime = nowTime - (day - 1) * oneDayLong;
			var TuesdayTime = nowTime - (day - 2) * oneDayLong;
			var WednesdayTime = nowTime - (day - 3) * oneDayLong;
			var ThursdayTime = nowTime - (day - 4) * oneDayLong;
			var FridayTime = nowTime - (day - 5) * oneDayLong;
			var SaturdayTime = nowTime - (day - 6) * oneDayLong;

			//			var SundayTime = nowTime + (7 - day) * oneDayLong;
			var sun = this.returnDateStr(new Date(SundayTime));
			var mon = this.returnDateStr(new Date(MondayTime));
			var tue = this.returnDateStr(new Date(TuesdayTime));
			var wed = this.returnDateStr(new Date(WednesdayTime));
			var thu = this.returnDateStr(new Date(ThursdayTime));
			var fri = this.returnDateStr(new Date(FridayTime));
			var sta = this.returnDateStr(new Date(SaturdayTime));
			switch(d) {
				case 0:
					this.day = sun;
					break;
				case 1:
					this.day = mon;
					break;
				case 2:
					this.day = tue;
					break;
				case 3:
					this.day = wed;
					break;
				case 4:
					this.day = thu;
					break;
				case 5:
					this.day = fri;
					break;
				case 6:
					this.day = sta;
					break;
				default:
					break;
			}
			console.log(sta);
			console.log(this.day);
		},
		returnDateStr: function(date) { // 日期转字符串
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var day = date.getDate();

			month = month <= 9 ? ('0' + month) : ('' + month);
			day = day <= 9 ? ('0' + day) : ('' + day);

			return year + month + day;
		},
		setDay: function(day) {
			this.getWeek(day)
			var day = day + 1;
			$(".week").removeClass("active");
			$(".week:nth-child(" + day + ")").addClass("active");
			//loading加载
			this.isShow = false;
			this.isS();
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.getClassList();
			}
		},
		getClassList: function() {
			var self = this;
			console.log(self.day);
			var data = {
				uid: self.uid,
				study_id: self.sid,
				day: self.day,
			};
			$.post(base + config.study_lesson_list, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.orderList = msg.data;
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//关闭本页面
		close: function() {
			api.closeWin();
		},
		//		进入视频房间
		enter: function(room, gid) {
			var self = this;
			console.log(self.uid);
			console.log(room);
			if(gid == 0) {
				api.toast({
					msg: "教师未开聊天窗口",
					duration: 2000,
					location: 'middle'
				});
			}
			api.sendEvent({
				name: 'videoMsg',
				extra: {
					room_name: room,
					user_id: self.uid,
					sid: self.sid,
					html_url: "file:///android_asset/widget/html/monitor.html"
				}
			});
		}
	}
})